/*
 * shmreader.cpp
 *
 *  Created on: Jul 14, 2013
 *      Author: m
 */

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include "NewSharedMemory.h"

using namespace ipc;

const int shmSize = 2;
const int dataPosition = 0;
const int writeCounterPosition = 1;

int main ( void )
{
    char mostrar[10240];
    int data = 0;
    int writeCounter = 0;
    int newWriteCounter = 0;

    SharedMemory<int> shm = SharedMemory<int>::get( "/tmp/myshm", 'a' );

    while ( data >= 0  )
    {

        newWriteCounter = shm.read( writeCounterPosition );

        if ( newWriteCounter != writeCounter )
        {
            data = shm.read( dataPosition );

            sprintf( mostrar,
                    "SHMREAD: "
                    "New data: %d.\n", data );
            write( fileno(stdout), mostrar, strlen(mostrar) );

            writeCounter = newWriteCounter;
        }
    }

    return 0;
}
